home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / schumann / clip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-09  |  2.6 KB  |  174 lines

  1. #include "extend.api"
  2. #include "fm.api"
  3.  
  4. /*
  5.  *  Listing 1
  6.  *
  7.  *  CLIP.C--
  8.  *
  9.  *     Examples of using C with Clipper.
  10.  *
  11.  *     SUM (nVal1, ..., nValn) --> nTotal
  12.  *       Return the sum of n integers.
  13.  *
  14.  *     REVERSE (cString) --> cString
  15.  *       Reverse a string.
  16.  *
  17.  *     PEEK (nSeg, nOff) --> nValue
  18.  *       Return the byte at address nSeg:nOff.
  19.  *
  20.  *     POKE (nSeg, nOff, nValue) --> nOrigValue
  21.  *       Set the byte at nSeg:nOff to nValue.
  22.  *
  23.  *     LRC (cString) --> nLRC
  24.  *       Simple Longitudinal Redundancy Checksum
  25.  *       of a string which may contain embedded NULLs.
  26.  *
  27.  *
  28.  *     Mark W. Schumann, September 1993
  29.  *     Use freely.
  30.  */
  31.  
  32. /*
  33.  *
  34.  *   SUM (nVal1, ..., nValn) --> nTotal
  35.  *
  36.  */
  37.  
  38. CLIPPER sum (void)
  39.  
  40. {
  41.  
  42. int pcount = _parinfo (0);
  43. int sum = 0;
  44. int i;
  45.  
  46.    for (i = 1; i <= pcount; i++)
  47.    {
  48.       sum += _parni (i);
  49.    }
  50.  
  51.    _retni (sum);
  52.  
  53. }
  54.  
  55. /*
  56.  *
  57.  *   REVERSE (cString) --> NIL
  58.  *
  59.  *   Returns string argument, reversed.
  60.  *
  61.  */
  62.  
  63. CLIPPER reverse (void)
  64.  
  65. {
  66.  
  67. char *oldstring;
  68. char *newstring;
  69. int len;
  70. int i;
  71.  
  72.    len = _parclen (1);
  73.    oldstring = _parc (1);
  74.    newstring = _xgrab (len);
  75.  
  76.    for (i = 0; i < len; i++)
  77.    {
  78.       newstring [i] = oldstring [len-i-1];
  79.    }
  80.  
  81.    _retclen (newstring, len);
  82.    _xfree (newstring);
  83.  
  84. }
  85.  
  86.  
  87. /*
  88.  *
  89.  *   PEEK (nSeg, nOff) --> nValue
  90.  *
  91.  *   Returns the byte at nSeg:nOff
  92.  *    without modifying it.
  93.  *
  94.  */
  95.  
  96. CLIPPER peek (void)
  97.  
  98. {
  99.  
  100. unsigned seg;
  101. unsigned off;
  102. unsigned char *p;
  103.  
  104.     seg = _parni (1);
  105.     off = _parni (2);
  106.  
  107.     p = (((unsigned long) seg) << 16)
  108.      | ((unsigned long) off);
  109.  
  110.     _retni ((int) *p);
  111.  
  112. }
  113.  
  114.  
  115. /*
  116.  *
  117.  *   POKE (nSeg, nOff, nValue) --> nOldValue
  118.  *
  119.  *   Sets the byte at nSeg:nOff to nValue,
  120.  *    and returns the previous value.
  121.  *
  122.  */
  123.  
  124. CLIPPER poke (void)
  125.  
  126. {
  127.  
  128. unsigned seg;
  129. unsigned off;
  130. unsigned char newvalue;
  131. unsigned char oldvalue;
  132. unsigned char *p;
  133.  
  134.     seg = _parni (1);
  135.     off = _parni (2);
  136.     value = _parni (3);
  137.  
  138.     p = (((unsigned long) seg) << 16)
  139.      | ((unsigned long) off);
  140.     oldvalue = *p;
  141.     *p = newvalue;
  142.  
  143.     _retni ((int) oldvalue);
  144.  
  145. }
  146.  
  147.  
  148. /*
  149.  *
  150.  *   LRC (cString) --> nLRC
  151.  *
  152.  *   Returns the cumulative XOR of a string parameter.
  153.  *
  154.  */
  155.  
  156. CLIPPER lrc (void)
  157.  
  158. {
  159.  
  160. int len = _parclen (1);
  161. unsigned char *s = _parc (1);
  162. unsigned char sum = 0;
  163.  
  164.     while (len > 0)
  165.     {
  166.         sum ^= *s++;  /* XOR each byte into */
  167.         len--;        /* the checksum.      */
  168.     }
  169.  
  170.     _retni ((int) sum);  /* Possibly non-portable  */
  171.                          /* on non-Intel platforms */
  172.  
  173. }
  174.